package com.shunli.cterminal.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.examples.core.enums.CommonStatusEnum;
import com.examples.core.exception.ServiceException;
import com.examples.core.mybatis.service.AbstractService;
import com.examples.core.tool.constant.ExamplesConstants;
import com.shunli.cterminal.entity.CollectionTemplate;
import com.shunli.cterminal.entity.CollectionTemplateDetail;
import com.shunli.cterminal.entity.SystemMall;
import com.shunli.cterminal.entity.SystemMallPlatform;
import com.shunli.cterminal.enums.CollectionStyleTypeEnum;
import com.shunli.cterminal.enums.OrderMallEnum;
import com.shunli.cterminal.mapper.CollectionTemplateMapper;
import com.shunli.cterminal.mapper.PromotionConfigMapper;
import com.shunli.cterminal.mapper.SystemMallMapper;
import com.shunli.cterminal.mapper.SystemMallPlatformMapper;
import com.shunli.cterminal.mapstruct.CollectionTemplateConverter;
import com.shunli.cterminal.model.bo.PromotionConfigBO;
import com.shunli.cterminal.model.code.CterminalResultCode;
import com.shunli.cterminal.model.res.StyleInfoVO;
import com.shunli.cterminal.service.CollectionTemplateDetailService;
import com.shunli.cterminal.service.CollectionTemplateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: CollectionTemplateServiceImpl
 * @description: TODO
 * @author: DAIYALU
 * @date: 2024/11/7
 * @version: 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CollectionTemplateServiceImpl extends AbstractService<CollectionTemplateMapper, CollectionTemplate> implements CollectionTemplateService {

    private final CollectionTemplateConverter templateConverter = CollectionTemplateConverter.INSTANCE;

    private final SystemMallPlatformMapper mallPlatformMapper;
    private final PromotionConfigMapper promotionConfigMapper;
    private final SystemMallMapper mallMapper;
    private final CollectionTemplateDetailService detailService;


    @Override
    public List<StyleInfoVO> get(String domainName, String goodsType, String channel) {
        String mallCode = OrderMallEnum.AD.getCode();
        if (StrUtil.isNotBlank(channel)) {
            List<PromotionConfigBO> platformList = promotionConfigMapper.queryPromotionConfigList(domainName, goodsType, channel);
            if (CollectionUtil.isNotEmpty(platformList) && platformList.size() == 1) {
                List<SystemMallPlatform> mallPlatformList = mallPlatformMapper.selectList(Wrappers.<SystemMallPlatform>lambdaQuery()
                        .eq(SystemMallPlatform::getPlatformCode, platformList.get(0).getPlatform()));
                if (CollectionUtil.isNotEmpty(mallPlatformList) && mallPlatformList.size() == 1) {
                    mallCode = mallPlatformList.get(0).getMallCode();
                }
            }
        }
        log.info("CollectionTemplateServiceImpl[]get[] 商城编码 mallCode:{}", mallCode);
        SystemMall mall = mallMapper.selectOne(Wrappers.<SystemMall>lambdaQuery()
                .eq(SystemMall::getCode, mallCode).last(ExamplesConstants.EXAM_MYSQL_LIMIT));
        if (null == mall) {
            throw new ServiceException(CterminalResultCode.COLLECTION_TEMPLATE_NOT_EXIST);
        }
        CollectionTemplate template = this.getOne(Wrappers.<CollectionTemplate>lambdaQuery()
                .eq(CollectionTemplate::getStatus, CommonStatusEnum.ENABLE.getCode())
                .eq(CollectionTemplate::getMallCode, mallCode)
                .orderByDesc(CollectionTemplate::getCreateTime)
                .last(ExamplesConstants.EXAM_MYSQL_LIMIT));
        if(null == template && !OrderMallEnum.AD.getCode().equals(mallCode)){
            template = this.getOne(Wrappers.<CollectionTemplate>lambdaQuery()
                    .eq(CollectionTemplate::getStatus, CommonStatusEnum.ENABLE.getCode())
                    .eq(CollectionTemplate::getMallCode, OrderMallEnum.AD.getCode())
                    .orderByDesc(CollectionTemplate::getCreateTime)
                    .last(ExamplesConstants.EXAM_MYSQL_LIMIT));
        }
        log.info("CollectionTemplateServiceImpl[]get[] 集合页模板 template:{}", JSON.toJSONString(template));
        if (null == template) {
            throw new ServiceException(CterminalResultCode.COLLECTION_TEMPLATE_NOT_EXIST);
        }
        List<CollectionTemplateDetail> detailList = detailService.list(Wrappers.<CollectionTemplateDetail>lambdaQuery()
                .eq(CollectionTemplateDetail::getTemplateId, template.getId()));
        log.info("CollectionTemplateServiceImpl[]get[] 集合页模板详情 detailList:{}", JSON.toJSONString(detailList));
        if (CollectionUtil.isEmpty(detailList)) {
            return Lists.newArrayList();
        }
        List<StyleInfoVO> styleInfoVOList = templateConverter.entityListToVOList(detailList);
        for (StyleInfoVO infoVo : styleInfoVOList) {
            infoVo.setTitle(CollectionStyleTypeEnum.ofCodeNoisy(infoVo.getStyleCode()).getDesc());
        }
        return styleInfoVOList;
    }

}
